package com.kejiguan.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.SelectNeItemForExhibitionActivity;
import com.kejiguan.ui.model.ExhibitionNeItem;

import java.util.List;

/**
 * Created by YYY on 16/7/18.
 */
public class ActivationExhibitionAdapter extends RecyclerView.Adapter<ActivationExhibitionVH> implements View.OnClickListener{

    private Context context;
    private List<ExhibitionNeItem> exhibitionNeItems;
    private LayoutInflater layoutInflater;
    private ExhibitionNeItem selectedExhibitionNeItem;

    public ActivationExhibitionAdapter(Context context,List<ExhibitionNeItem> exhibitionNeItems)
    {
        this.context=context;
        this.exhibitionNeItems=exhibitionNeItems;
        layoutInflater=LayoutInflater.from(context);
    }


    @Override
    public ActivationExhibitionVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=layoutInflater.inflate(R.layout.activity_activation_exhibition_item_layout,null);
        return new ActivationExhibitionVH(itemView);
    }

    @Override
    public void onBindViewHolder(ActivationExhibitionVH holder, int position) {

        ExhibitionNeItem exhibitionNeItem=exhibitionNeItems.get(position);
        holder.exhibitionIdView.setText(exhibitionNeItem.getExhibitionId());
        holder.exhibitionNameView.setText(exhibitionNeItem.getExhibitionName());
        if (null!=selectedExhibitionNeItem&&exhibitionNeItem.getExhibitionId().equals(selectedExhibitionNeItem.getExhibitionId()))
        {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_checked);
            holder.selectNeBtn.setVisibility(View.VISIBLE);
            holder.selectNeBtn.setClickable(true);
        }else
        {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_uncheck);
            holder.selectNeBtn.setVisibility(View.INVISIBLE);
            holder.selectNeBtn.setClickable(false);
        }
        holder.selectStateView.setTag(R.id.recyclerview_item_position,position);
        holder.selectNeBtn.setTag(R.id.recyclerview_item_position,position);
        holder.selectStateView.setOnClickListener(this);
        holder.selectNeBtn.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {

        if(exhibitionNeItems==null)
        {
            return 0;
        }else
        {
            return exhibitionNeItems.size();
        }

    }

    public void setExhibitionNeItems(List<ExhibitionNeItem> exhibitionNeItems) {
        selectedExhibitionNeItem=null;
        this.exhibitionNeItems = exhibitionNeItems;
        notifyDataSetChanged();
    }

    public ExhibitionNeItem getSelectedExhibitionNeItem() {
        return selectedExhibitionNeItem;
    }

    @Override
    public void onClick(View v) {

        int position= (int) v.getTag(R.id.recyclerview_item_position);
        switch (v.getId())
        {
            case R.id.select_state:

                if (selectedExhibitionNeItem==exhibitionNeItems.get(position))
                {
                    selectedExhibitionNeItem=null;
                }else
                {
                    selectedExhibitionNeItem=exhibitionNeItems.get(position);
                }

                notifyDataSetChanged();
                break;
            case R.id.select_ne:
                ExhibitionNeItem exhibitionNeItem=exhibitionNeItems.get(position);
                Intent intent=new Intent(context, SelectNeItemForExhibitionActivity.class);
                intent.putExtra("exhibitionId",exhibitionNeItem.getExhibitionId());
                intent.putExtra("exhibitionName",exhibitionNeItem.getExhibitionName());
                intent.putExtra("ids",exhibitionNeItem.getResource());
                ((Activity)context).startActivityForResult(intent,99);
                break;
        }

    }


    public List<ExhibitionNeItem> getExhibitionNeItems() {
        return exhibitionNeItems;
    }
}
